package com.lewis.core.utils.office.excel;

import lombok.Data;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.xssf.usermodel.XSSFColor;

import java.awt.*;
import java.io.Serializable;

/**
 * excel实体
 *
 * @author Lewis
 */
@Data
public class Grid implements Serializable {

	private static final long serialVersionUID = 1L;

	private boolean show;

	/**
	 * 对应Excel中的row,也可以理解为cells[i][j]的i
	 */
	private int row;

	/**
	 * 对应Excel中的col,也可以理解为cells[i][j]的j
	 */
	private int col;

	/**
	 * x坐标
	 */
	private int x;

	/**
	 * y坐标
	 */
	private int y;

	private int width;

	private int height;

	private String text;

	private Font font;

	private Color bgColor = null;

	private Color ftColor = null;

	public Grid() {
		super();
	}

	/**
	 * 将poi.ss.usermodel.Color 转换成 java.awt.Color
	 * <a href="http://home.cnblogs.com/u/309701/" target="_blank">@param</a>
	 * color
	 */
	public void setBgColor(org.apache.poi.ss.usermodel.Color color) {
		this.bgColor = poiColor2awtColor(color);
	}

	public void setFtColor(org.apache.poi.ss.usermodel.Color color) {
		this.ftColor = poiColor2awtColor(color);
	}

	public void setFont(org.apache.poi.ss.usermodel.Font font) {
		if (font != null) {
			this.font = new Font(font.getFontName(), Font.BOLD, font.getFontHeight() / 20 + 2);
		}
	}

	private Color poiColor2awtColor(org.apache.poi.ss.usermodel.Color color) {
		Color awtColor = null;
		if (color instanceof XSSFColor) { // .xlsx
			XSSFColor xc = (XSSFColor) color;
			String rgbHex = xc.getARGBHex();
			if (rgbHex != null) {
				awtColor = new Color(Integer.parseInt(rgbHex.substring(2), 16));
			}
		} else if (color instanceof HSSFColor) { // .xls
			HSSFColor hc = (HSSFColor) color;
			short[] s = hc.getTriplet();
			if (s != null) {
				awtColor = new Color(s[0], s[1], s[2]);
			}
		}
		return awtColor;
	}
}
